package com.community.handler;

import com.community.entity.Pay;
import com.community.json.JsonResult;
import com.community.service.PayService;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("html/pay")
public class PayHandler {
    @Resource
    private PayService payService;

    @GetMapping("findAll")
    public JsonResult findAll(Integer page){
        System.out.println("查询所有的缴费信息");
        System.out.println("当前页为"+page);
        JsonResult jsonResult = new JsonResult();
        if(page == null){
            page = 1;
        }
        Integer start = (page-1)*5;
        List<Pay> payList = payService.findAll(start);
        Integer count = payService.findCount(null);
        System.out.println("pay表的总长度为"+count);
        Integer maxPage = count/5;
        if(count%5 != 0){
            maxPage += 1;
        }
        jsonResult.setPage(page);
        jsonResult.setMaxPage(maxPage);
        jsonResult.setHttpCode(200);
        jsonResult.setData(payList);
        return jsonResult;
    }

    @GetMapping("findByState")
    public JsonResult findByState(Integer state,Integer page){
        System.out.println("根据状态查询缴费信息，状态为"+state);
        System.out.println("当前页为"+page);
        JsonResult jsonResult = new JsonResult();
        if(page == null){
            page = 1;
        }
        Integer start = page*5-5;
        List<Pay> payList = payService.findByState(state,start);
        Integer count = payService.findCount(state);
        System.out.println("pay表的总长度为"+count);
        Integer maxPage = count/5;
        if(count%5 != 0){
            maxPage += 1;
        }
        jsonResult.setPage(page);
        jsonResult.setMaxPage(maxPage);
        jsonResult.setHttpCode(200);
        jsonResult.setData(payList);
        return jsonResult;
    }

    @DeleteMapping("deleteById")
    public JsonResult deleteById(Integer[] payIdList){
        return null;
    }
}
